﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using uMES.LeanManufacturing.ReportBusiness;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;


public partial class Login : System.Web.UI.Page
{
    private uMESLoginBusiness bll = new uMESLoginBusiness();

   
    ADHelper adh = new ADHelper();
    private string giLoginUserName = "";
    private string password = "";
    private string strEncryPwd = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        giLoginUserName = Request.QueryString["giLoginUserName"] == null ? "" : Request.QueryString["giLoginUserName"].ToString();
        password = Request.QueryString["pwd"] == null ? "" : EncryptAndDecryptTool.DesDecrypt(Request.QueryString["pwd"].ToString());
        strEncryPwd = adh.Encrypto(password);
        if (!IsPostBack)
        {
            AutoLonin(giLoginUserName,strEncryPwd,password);
        }
    }

    //从MES系统登录
    protected void AutoLonin(string username,string strEncryPwd, string pwd)
    {
        if (username != "")
        {
            string info = "";
            bool bol = bll.Login(username, strEncryPwd, pwd, out info);
            if (bol)
            {
                DataTable dt = bll.GetLoginInfo(username);

                //登录成功将员工id赋值给文板框
                DataTable dtEmployeeInfo = bll.getEmployeeInfo(username);
                txtEmployeeid.Text = dtEmployeeInfo.Rows[0]["employeeid"].ToString();//用户id
                string factoryName = dtEmployeeInfo.Rows[0]["factoryname"].ToString();// 用户所在车间名
                string factoryId = dtEmployeeInfo.Rows[0]["factoryid"].ToString();// 用户所在车间ID
                string workcenter = dtEmployeeInfo.Rows[0]["workcentername"].ToString();// 用户所在工段名
                string workcenterId = dtEmployeeInfo.Rows[0]["workcenterid"].ToString();// 用户所在工段id
                string fullname = dtEmployeeInfo.Rows[0]["fullname"].ToString();//用户名
                string teamName = dtEmployeeInfo.Rows[0]["teamname"].ToString();// 用户所在班组
                string teamId = dtEmployeeInfo.Rows[0]["teamid"].ToString();// 用户所在班组id

                //用自定义dto类传值
                EmployeeInfoDTO dto = new EmployeeInfoDTO();
                dto.Employeeid = txtEmployeeid.Text;
                dto.EmployeeName = fullname;
                dto.FactoryName = factoryName;
                dto.Workcenter = workcenter;
                dto.FactoryId = factoryId;
                dto.WorkcenterId = workcenterId;
                if (string.IsNullOrEmpty(teamName))
                {
                    dto.TeamName = "";
                }
                else
                {
                    dto.TeamName = teamName;
                }
                if (string.IsNullOrEmpty(teamId))
                {
                    dto.TeamId = "";
                }
                else
                {
                    dto.TeamId = teamId;
                }
          
                Session["loginInfo"] = dt;

                
                Session["employeeinfo"] = dto;
                Response.Redirect("index.html");
            }

        }
    }

    //点击"登录"按钮方式登录
    protected void loginButton_Click(object sender, EventArgs e)
    {
        string strUid = this.userNameTxtBox.Text;
        string strEncryPwd = adh.Encrypto(this.passwordTxtBox.Text);// 解密后的密码
        string password = this.passwordTxtBox.Text;                 // 用户输入的原始密码

        string info = "";
        bool bol = bll.Login(strUid, strEncryPwd,password, out info);

        if (bol)
        {
            DataTable dt = bll.GetLoginInfo(strUid);

            // 登录成功将员工id赋值给文板框
            DataTable dtEmployeeInfo = bll.getEmployeeInfo(strUid);
            txtEmployeeid.Text = dtEmployeeInfo.Rows[0]["employeeid"].ToString();//用户id
            string factoryName = dtEmployeeInfo.Rows[0]["factoryname"].ToString();// 用户所在车间名
            string factoryId = dtEmployeeInfo.Rows[0]["factoryid"].ToString();// 用户所在车间ID
            string fullname = dtEmployeeInfo.Rows[0]["fullname"].ToString();//用户名
            string teamName = dtEmployeeInfo.Rows[0]["teamname"].ToString();// 用户所在班组
            string teamId = dtEmployeeInfo.Rows[0]["teamid"].ToString();// 用户所在班组id
            string strRealmName = dtEmployeeInfo.Rows[0]["employeename"].ToString();

            EmployeeInfoDTO dto = new EmployeeInfoDTO();
            dto.RealmName = strRealmName;
            dto.Employeeid = txtEmployeeid.Text;
            dto.EmployeeName = fullname;
            dto.FactoryName = factoryName;
            dto.FactoryId = factoryId;
            if (string.IsNullOrEmpty(teamName))
            {
                dto.TeamName = "";
            }
            else
            {
                dto.TeamName = teamName;
            }
            if (string.IsNullOrEmpty(teamId))
            {
                dto.TeamId = "";
            }
            else
            {
                dto.TeamId = teamId;
            }
            //dto.DtPages = dtPages;

            Session["loginInfo"] = dt;

  
            Session["employeeinfo"] = dto;

            HttpCookie cookie = new HttpCookie("current_LoginUserName");
            TimeSpan span = new TimeSpan(30, 0, 0, 0);
            cookie.Expires = DateTime.Now + span;
            cookie.Values.Add("LoginUserName", strUid);
            Response.AppendCookie(cookie);
            Response.Redirect("index.html");
        }
        else
        {
            Response.Write("<script type='text/javascript'>alert('" + info + "');</script>");
        }
    }
}
